from flask import request, make_response
from datetime import datetime
from utils import utils
import redis
import time

class AdminStations:
    @staticmethod
    def get_station_list():
        db = redis.Redis(host="localhost", port=6379, decode_responses=True)
        id_list = db.smembers("station_list")

        res = {
            'num': len(id_list),
            'stations': []
        }

        if res['num'] != 0:
            for station_id in id_list:
                station = db.hgetall(station_id)
                res_s = {
                    'id': station_id,
                    'type': station['type'],
                    'is_on': int(station['is_on']),
                    'is_serve': int(station['is_serve']),
                    'serves': int(station['serves']),
                    'time_finish': int(station['time_finish']),
                    'power_finish': int(station['power_finish']),
                    'serve_id': station['serve_id'] if 'server_id' in station else '0'
                }

                if res_s['is_serve'] == 1:
                    last_time = float(station['last_serve'])
                    now_time = utils.get_system_time()
                    minutes = int(now_time - last_time) // 60
                    res_s['time_finish'] = res_s['time_finish'] + minutes
                    res_s['power_finish'] = res_s['power_finish'] + utils.get_power_rate(res_s['type']) * minutes / 60

                res['stations'].append(res_s)

        return make_response(res)

    @staticmethod
    def get_station_query():
        db = redis.Redis(host="localhost", port=6379, decode_responses=True)
        station_id = request.args.get('name')

        charge_queue = utils.get_charge_queue(station_id)
        res = {
            'id': station_id,
            'num': len(charge_queue),
            'queue': []
        }

        for idx, user_id in enumerate(charge_queue):
            order_id = db.hget(user_id, 'order')
            order = db.hgetall(order_id)
            user_info = {
                'queue_id': idx + 1,
                'user_id': user_id,
                'capcity': order['capcity'] if order['capcity'] else 0,
                'request_power': order['request'] if order['request'] else 0,
                'time_in_queue': order['time_in_queue'] if order['time_in_queue'] else 0.0
            }
            time_in_queue = float(user_info['time_in_queue'])
            now_time = utils.get_system_time()
            minutes = int(now_time - time_in_queue) // 60
            user_info['time_in_queue'] = minutes
            res['queue'].append(user_info)

        return make_response(res)

    @staticmethod
    def get_report_list():
        db = redis.Redis(host='localhost', port=6379, decode_responses=True)
        report_keys = db.keys(pattern="REPORT_*")

        res = {
            'num': len(report_keys),
            'stations': []
        }
        for key in report_keys:
            db_res = db.hgetall(key)
            report = {
                'id': db_res['id'],
                'date': db_res['date'],
                'all_serves': int(db_res['all_serves']),
                'all_time': int(db_res['all_time']),
                'all_power': float(db_res['all_power']),
                'all_power_fees': float(db_res['all_power_fees']),
                'all_serve_fees': float(db_res['all_serve_fees']),
                'all_fees': float(db_res['all_fees']),
            }
            res['stations'].append(report)
        return make_response(res)
